package com.lagou.server;

import com.lagou.client.ClientScanner;
import com.lagou.model.Student;

import java.io.*;
import java.util.ArrayList;

public class StudentModule {
    //管理员添加
    public void add(Student s,ArrayList<Student> arrayList) {
            arrayList.add(s);
    }
    //管理员删除
    public void delete(Student s,ArrayList<Student> arrayList) {
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i).getId()==s.getId()){
                arrayList.remove(i);
                break;
            }
        }
        System.out.println("删除成功");
    }
    //管理员查询
    public Student findOF(Student s,ArrayList<Student> arrayList) {
        Student student = null;
        boolean flag = false;
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i).getId()==s.getId()){
                System.out.println("查询成功！");
                System.out.println("该学生为"+arrayList.get(i));
                student=arrayList.get(i);
                flag=true;
                break;
            }
            flag=false;
        }if (!flag){
            System.out.println("该学生不存在！");
        }
        return student;
    }
    //管理员修改
    public void update(Student s,ArrayList<Student> arrayList) {
        Boolean flag=false;
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i).getId()==s.getId()){
                arrayList.get(i).setName(s.getName());
                arrayList.get(i).setPassword(s.getPassword());
                arrayList.get(i).setMajor(s.getMajor());
                flag=true;
                break;
            }
        }
        if (flag){
            System.out.println("修改成功!");
        }else {
            System.out.println("该学生不存在！");
        }
    }
    //学生修改密码
    public  void updatePWD(Student s,ArrayList<Student> arrayList){
        Boolean flag=false;
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i).getId()==s.getId()){
             arrayList.get(i).setPassword(s.getPassword());
             flag=true;
                break;
            }
        }
        if (flag){
            System.out.println("修改成功!");
        }else {
            System.out.println("该学生不存在！");
        }
    }

    //从硬盘读入信息
    public void read(ArrayList<Student> arrayList) throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("G:/file.txt"));
        ArrayList<Student> a=(ArrayList<Student>) objectInputStream.readObject();

        for (Student s:a
        ) {
            arrayList.add(s);
        }
        System.out.println("学生信息加载成功");
    }
    //退出后学生信息系统，保存学生信息在硬盘里
    public void save(ArrayList<Student> arrayList) throws IOException {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("G:/file.txt"));
        objectOutputStream.writeObject(arrayList);
        System.out.println("学生信息成功保存到硬盘的文件中");


    }

}
